package com.gaff.emp.core.mail;

import cn.hutool.core.util.StrUtil;
import com.gaff.emp.core.modules.sys.annotation.Dict;
import com.gaff.emp.core.modules.sys.entity.SysDict;
import com.gaff.emp.core.modules.sys.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.InputStreamSource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.util.List;

@Component
@Slf4j
public class MailService implements ApplicationContextAware, InitializingBean {

    private ApplicationContext applicationContext;

    private JavaMailSenderImpl mailSender;

    @Autowired
    private SysDictService sysDictService;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void sendSimpleMessage(String subject, String text) {
        List<SysDict> list = sysDictService.queryDictByKey("mail.receiver");
        if (list == null || list.size() == 0 || mailSender == null) {
            log.warn("sendSimpleMessage failed, Please check your mail configuration");
            return;
        }
        for (SysDict dict : list) {
            String receiver = dict.getDictValue();
            if (StrUtil.isBlank(receiver)) {
                continue;
            }
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(mailSender.getUsername());
            message.setTo(receiver);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
        }
    }

    public void sendMimeMessage(String subject, String text, String attachmentName, InputStreamSource source) {
        List<SysDict> list = sysDictService.queryDictByKey("mail.receiver");
        if (list == null || list.size() == 0 || mailSender == null) {
            log.warn("sendMimeMessage failed, Please check your mail configuration");
            return;
        }
        for (SysDict dict : list) {
            String receiver = dict.getDictValue();
            if (StrUtil.isBlank(receiver)) {
                continue;
            }
            try {
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message);
                helper.setFrom(mailSender.getUsername());
                helper.setTo(receiver);
                helper.setSubject(subject);
                helper.setText(text);
                helper.addAttachment(attachmentName, source);
                mailSender.send(message);
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void afterPropertiesSet() {
        if (!applicationContext.containsBean("mailSender")) {
            log.warn("Mail module is disabled");
            return;
        }
        mailSender = applicationContext.getBean(JavaMailSenderImpl.class);
        mailSender.getHost();
        StringBuilder sb = new StringBuilder();
        sb.append("\n==================Mail Config======================");
        sb.append("Mail Host : " + mailSender.getHost());
        sb.append("Mail Port : " + mailSender.getPort());
        sb.append("Mail Username : " + mailSender.getUsername());
        sb.append("Mail Password : " + mailSender.getPassword());
        log.info(sb.toString());
    }
}
